package cn.cmcc.online.smsapi.ui.presenter;

import android.view.View;
import android.widget.TableRow;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.cmcc.online.smsapi.ui.model.CommonData;
import cn.cmcc.online.smsapi.ui.util.DensityUtil;
import cn.cmcc.online.smsapi.ui.util.ItemRepertory;
import cn.cmcc.online.smsapi.ui.util.ItemRepertoryBaseImpl;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static cn.cmcc.online.smsapi.ui.view.GeneralBaseVistaImpl.WITH_ANIMATION;

/**
 * Created by ZhangYifan on 2017/6/16.
 */

public class CommonPresenterImpl extends CommonPresenter {
    private CommonData mData;

    private Map<String, String> mListInfo;
    private ItemRepertory<TableRow> mTableRowRepertory;
    private List<TableRow> mShowedTableRows;

    private boolean mIsFirst;

    public CommonPresenterImpl() {
        mListInfo = new LinkedHashMap<>();
        mTableRowRepertory = new TableRowRepertory();
    }

    @Override
    public void bind(JSONObject obj) {
        super.bind(obj);

        mData = new CommonData(obj);
        String highlightTitle = mData.getHighInfoTitle();
        String highlightContent = mData.getHighInfoValue();
        Map<String, String> importantInfo = mData.getMainInfoDadaArr();
        Map<String, String> lessImportInfo = mData.getExtraInfoDataArr();
        String notice = mData.getExtraInfoTip();

        if (highlightTitle != null && highlightTitle.length() > 0 && highlightContent != null && highlightContent.length() > 0) {
            vista.setHighInfoVisible(VISIBLE);
            vista.setHighInfo(mData.getColor());
            vista.setHighInfo(highlightTitle, highlightContent);
        } else {
            vista.setHighInfoVisible(GONE);
        }

        mListInfo.clear();
        if (importantInfo != null && importantInfo.size() > 0) {
            mListInfo.putAll(importantInfo);
        }
        if (lessImportInfo != null && lessImportInfo.size() > 0) {
            mListInfo.putAll(lessImportInfo);
        }

        updateList();

        if (notice != null && notice.length() > 0) {
            vista.setTipInfoVisible(VISIBLE);
            vista.setTipInfo(notice);
        } else {
            vista.setTipInfoVisible(GONE);
        }
    }

    private void updateList() {
        if (mListInfo.size() > 0) {
            mShowedTableRows = new ArrayList<>();
            Iterator<Map.Entry<String, String>> iterator = mListInfo.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                String title = entry.getKey();
                StringBuilder contentBuilder = new StringBuilder(entry.getValue());
                int index = 15;
                while (index < contentBuilder.length()) {
                    contentBuilder.insert(index, "\n");
                    index += 15;
                }

                TableRow tableRow = mTableRowRepertory.getOrCreateItem();
                mShowedTableRows.add(vista.updateTableRow(tableRow, title, contentBuilder.toString()));
            }

            if (mShowedTableRows.size() > 0) {
                vista.setListInfoVisible(VISIBLE);
                vista.setListInfo(mShowedTableRows);
            }
            if (mListInfo.size() > 5) {
                vista.setExpandButtonVisible(VISIBLE);
                vista.setExpandButtonColor(mData.getColor());
                mIsFirst = true;
                showLess();
                mIsFirst = false;
            } else {
                vista.setExpandButtonVisible(GONE);
            }
        } else {
            vista.setListInfoVisible(GONE);
            vista.setExpandButtonVisible(GONE);
        }
    }

    @Override
    public void showMore() {
        int targetHeight = 0;
        for (TableRow tableRow : mShowedTableRows) {
            tableRow.measure(View.MeasureSpec.makeMeasureSpec(DensityUtil.dp2px(tableRow.getContext(), 300), View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            int height = tableRow.getMeasuredHeight();
            targetHeight += height;
        }
        vista.setTableLayoutHeight(targetHeight, WITH_ANIMATION);
    }

    @Override
    public void showLess() {
        int count = 0;
        int targetHeight = 0;
        for (TableRow tableRow : mShowedTableRows) {
            if (count == 5) {
                break;
            }
            int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            tableRow.measure(0, h);
            int height = tableRow.getMeasuredHeight();
            targetHeight += height;
            count++;
        }
        if (mIsFirst) {
            vista.setTableLayoutHeight(targetHeight, false);
        } else {
            vista.setTableLayoutHeight(targetHeight, WITH_ANIMATION);
        }
    }

    class TableRowRepertory extends ItemRepertoryBaseImpl<TableRow> {

        @Override
        public TableRow createItem() {
            return vista.createTableRow();
        }
    }
}
